<template>
  <div class="password-change-container">
    <el-card class="password-change-card">
      <template #header>
        <div class="card-header">
          <span>修改密码</span>
        </div>
      </template>
      <el-form 
        :model="passwordForm" 
        :rules="rules" 
        ref="passwordFormRef" 
        label-width="100px"
        label-position="top"
      >
        <el-form-item label="原密码" prop="oldPassword">
          <el-input v-model="passwordForm.oldPwd" type="password" placeholder="请输入原密码" show-password />
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="passwordForm.newPwd" type="password" placeholder="请输入新密码" show-password />
        </el-form-item>
        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input v-model="passwordForm.newPwd1" type="password" placeholder="请再次输入新密码" show-password />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm" :loading="loading">确认修改</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { editAccount } from '@/api/account'

const user=JSON.parse(sessionStorage.getItem('user'));
// 表单数据
const passwordForm = ref({
 
  newPwd: '',
  id: user.id,
   newPwd1: '',
   oldPwd:'',
})
// 表单引用
const passwordFormRef = ref(null)
// 加载状态
const loading = ref(false)

// 验证规则
const rules = {
  oldPwd: [
    { required: true, message: '请输入原密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ],
  newPwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' },
    { validator: (rule, value, callback) => {
        if (value === passwordForm.value.oldPwd) {
          callback(new Error('新密码不能与原密码相同'));
        } else {
          callback();
        }
      }, trigger: 'blur' }
  ],
  newPwd1: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    { validator: (rule, value, callback) => {
        if (value !== passwordForm.value.newPwd) {
          callback(new Error('两次输入的密码不一致'));
        } else {
          callback();
        }
      }, trigger: 'blur' }
  ]
};
// 提交表单
const submitForm = () => {

  passwordFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
       console.log(passwordForm.value)
  editAccount(passwordForm.value).then(res => {
    console.log(res)
    ElMessage.success('修改成功，请重新登录')
  })
    }
    else {
      ElMessage.error('表单验证失败，请检查输入')
    }
  });
 
}

// 重置表单
const resetForm = () => {
  passwordFormRef.value.resetFields()
}
</script>

<style scoped>
.password-change-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;

}

.password-change-card {
  width: 450px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  font-size: 18px;
  font-weight: bold;
  text-align: center;
}

.el-form-item {
  margin-bottom: 22px;
}

.el-button {
  width: 100%;
  margin-bottom: 10px;
}

.el-button + .el-button {
  margin-left: 0;
}
</style>